package com.edu.service.impl;

import com.edu.bo.PaperGengerateVO;
import com.edu.bo.TypeTotalVO;
import com.edu.component.PageBean;
import com.edu.component.QueryParam;
import com.edu.dao.PaperMapper;
import com.edu.dao.QuestionMapper;
import com.edu.entity.Paper;
import com.edu.entity.PaperQuestion;
import com.edu.entity.Question;
import com.edu.service.PaperService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PaperServiceImpl implements PaperService {
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private QuestionMapper questionMapper;

    //1.新增试卷
    @Override
    public void savePaper(Paper paper) {
        int updateCnt = paperMapper.savePaper(paper);
        if ( updateCnt!= 1 ){
            throw new RuntimeException("新增试卷失败.");
        }

    }

    //2.更新试卷
    @Override
    public void updatePaper(Paper paper) {
        int updateCnt = paperMapper.updatePaper(paper);
        if ( updateCnt!= 1 ){
            throw new RuntimeException("更新试卷失败.");
        }

    }

    //3.删除试卷
    @Override
    public void deletePaper(Integer id) {
        //1.如果这个试卷是已经组卷了, 不可以删除它
        //   返回异常消息: 这个试卷被组卷了, 不可以删除
        if (isPaperGrouped(id)){
            throw new RuntimeException("该试卷已被组卷，不可删除！");
        }
        //2.系统没有组卷关联, 可以删除
        int deleteCnt=paperMapper.deleteNoPaperQuestion(id);
        //3.如果删除失败, 报出异常，返回异常消息
        if (deleteCnt!=1){
            throw new RuntimeException("删除试卷失败");
        }

    }
    private boolean isPaperGrouped(Integer paperId) {
        List<PaperQuestion> paperQuestions = paperMapper.queryQuestionByPaperId(paperId);
        // 如果列表不为空，表示试卷已经被组卷
        return !paperQuestions.isEmpty();
    }

    //4.试卷清单
    @Override
    public PageBean listPage(QueryParam param) {
        //1.开启分页拦截功能
        Page retPage = PageHelper.startPage(param.getPage(), param.getLimit(), true);
        //2.执行查询
        paperMapper.queryList(param.getParam());
        //3.返回PageBean对象
        return new PageBean(retPage);
    }

    @Override
    public void diyPaperQuestion(PaperQuestion paperQuestion) {
        //1.先删除数据库中与当前试卷关联的题目
        Integer paperId = paperQuestion.getPaperId();
        paperMapper.deletePaperQuestion(paperId);
        //2.获取对象中所有的题目ID
        List<Question> idsList = paperQuestion.getQuestionIdsList();
        //2.1-map可以将Question转为一个Map
        //2.2-collect把map转为一个List
        List<Map> params = idsList.stream().map(
                (Q) -> {
                    HashMap map = new HashMap();
                    map.put("paperId",paperId);
                    map.put("questionId",Q.getId());
                    return map;
                }
        ).collect( Collectors.toList() );
        //3.执行批量插入, 把关联信息写入数据库
        paperMapper.insertBatchPaperQuestion(params);
    }

    //查询试卷
    @Override
    public List<Paper> queryPaper() {
        return paperMapper.queryPaper();
    }

    //根据试卷ID来查询问题
    @Override
    public List<PaperQuestion> queryQuestionByPaperId(Integer paperId) {
        return paperMapper.queryQuestionByPaperId(paperId);
    }

    //预览试卷
    @Override
    public PaperGengerateVO previewPaper(Integer paperId) {
        return paperMapper.previewPaper(paperId);
    }

    @Override
    public void randomPaperQuestion(Map map) {

    }

    @Override
    public List<TypeTotalVO> queryTypeTotal() {
        return null;
    }

    @Override
    public List<Paper> queryAll() {
        return null;
    }
}
